package com.rrd.itext.xmlBean.layoutBean;

import com.itextpdf.text.Chunk;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.rrd.itext.obj.ResouceObject;
import com.rrd.itext.utils.PDFBuilder;
import com.rrd.itext.utils.StyleUtil;
import com.rrd.itext.utils.enumeration.AlignStyle;
import com.rrd.itext.utils.enumeration.FontStyle;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementParser;
import com.rrd.itext.xmlBean.layoutBean.iface.PdfElementRender;
import com.rrd.itext.xmlBean.layoutBean.iface.PrintOrder;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("Chunk")
public class XmlChunk extends XmlFontStyle implements PrintOrder, PdfElementRender, PdfElementParser<Chunk>{
	@XStreamAsAttribute
	private String align;
	@XStreamAsAttribute
	private String postionType;
	@XStreamAsAttribute
	private float offsetX;
	@XStreamAsAttribute
	private float offsetY;
	@XStreamAsAttribute
	private String newLine;
	@XStreamAsAttribute
	private int printOrder;
	
	@XStreamAlias("text")
	private XmlText text;
	@XStreamAlias("Anchor")
	private XmlAnchor anchor;
	@XStreamAlias("HTMLBlock")
	private XmlHtmlBlock htmlBlock;
	
	public boolean hasTextContent(){
		return text != null;
	}
	
	public int getAlign() {
		return AlignStyle.getAlign(this.align);
	}



	public void setAlign(String align) {
		this.align = align;
	}



	public String getPostionType() {
		return postionType;
	}



	public void setPostionType(String postionType) {
		this.postionType = postionType;
	}



	public float getOffsetX() {
		return offsetX;
	}


	public float getOffsetY() {
		return offsetY;
	}



	public void setOffsetY(float offsetY) {
		this.offsetY = offsetY;
	}



	public void setOffsetX(float offsetX) {
		this.offsetX = offsetX;
	}


	public XmlHtmlBlock getHtmlBlock() {
		return htmlBlock;
	}



	public void setHtmlBlock(XmlHtmlBlock htmlBlock) {
		this.htmlBlock = htmlBlock;
	}

	public int getPrintOrder() {
		return printOrder;
	}


	public void setPrintOrder(int printOrder) {
		this.printOrder = printOrder;
	}


	public boolean getNewLine() {
		if(newLine == null || (!newLine.equalsIgnoreCase("true")))
			return false;
		return true;
	}


	public void setNewLine(String newLine) {
		this.newLine = newLine;
	}

	public XmlAnchor getAnchor() {
		return anchor;
	}

	public void setAnchor(XmlAnchor anchor) {
		this.anchor = anchor;
	}

    public XmlText getText() {
        return text;
    }

    public void setText(XmlText text) {
        this.text = text;
    }

    @Override
    public void render(PdfContentByte cb, PDFBuilder builder) throws Exception {
        if (this.hasTextContent()) {
            cb.beginText();
            Font f = StyleUtil.createFont(this, builder.getStyleMap());
            XmlText text = this.getText();
            String content = text.getText();
            if(content == null || "".equals(content)){
                String name = text.getName();
                if(name == null){
                    return;
                }
                if(!builder.getResourceMap().containsKey(name)){
                    return;
                }
                content = ResouceObject.getDataContent(builder.getResourceMap(), name); //builder.getResourceMap().get(name);
            }
            if(content == null){
                return;
            }
            Phrase p = new Phrase(content, f);
            ColumnText.showTextAligned(cb, this.getAlign(), p, this.getOffsetX(), this.getOffsetY(), 0f);
            cb.endText();
        }
    }

    @Override
    public Chunk parse(PDFBuilder builder) throws Exception {
        Font chunkFont = StyleUtil.createFont(this, builder.getStyleMap(), builder.getFontMap());
        if(this.hasTextContent()){
            XmlText text = this.getText();
            String content = text.getText();
            if(content == null || "".equals(content)){
                String name = text.getName();
                if(name == null){
                    return null;
                }
                if(!builder.getResourceMap().containsKey(name)){
                    return null;
                }
                content = ResouceObject.getDataContent(builder.getResourceMap(), name);
            }
            if(content == null){
                return null;
            }
            Chunk ck = new Chunk(content, chunkFont);
            return ck;
        }else if(this.getHtmlBlock() != null){
            String name = this.getHtmlBlock().getName();
            if(builder.getResourceMap().containsKey(name)){
                String content = ResouceObject.getDataContent(builder.getResourceMap(), name);
                Chunk ck = new Chunk(content, chunkFont);
                return ck;
            }
        }
        return null;
    }

}
